package cn.sciento.farm.devicemanager.api.jsonrpc.impl;


import cn.sciento.farm.devicemanager.api.jsonrpc.InverterJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevInverter;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttBlockedSendManager;
import cn.sciento.farm.devicemanager.gateway.downlink.service.impl.MqttSendService;
import cn.sciento.farm.devicemanager.gateway.model.downlink.InverterControlData;
import cn.sciento.farm.devicemanager.service.InverterService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AutoJsonRpcServiceImpl
public class InverterJsonApiImpl implements InverterJsonApi {

    @Autowired
    InverterService inverterService;

    @Autowired
    MqttBlockedSendManager mqttBlockedSendManager;

    @Autowired
    MqttSendService mqttSendService;

    @Override
    public ResponseMessage create(DevInverter inverter) {
        if(StringUtils.isEmpty(inverter.getName())
                || StringUtils.isEmpty(inverter.getEui())
                || StringUtils.isEmpty(inverter.getLocation()
        )){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevInverter oldInverter = inverterService.getByEui(inverter.getEui());
            if(oldInverter != null){
                return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
            }else {
                DevInverter devInverter = new DevInverter();
                BeanUtils.copyProperties(inverter,devInverter);
                devInverter.setCreateTime(System.currentTimeMillis());
                devInverter.setUpdateTime(System.currentTimeMillis());
                int count = inverterService.insert(devInverter);
                return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,
                        count,inverter);
            }
        }
    }

    @Override
    public ResponseMessage update(DevInverter inverter) {
        if(inverter.getId() <= 0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevInverter devInverter = new DevInverter();
            devInverter.setId(inverter.getId());
            devInverter.setLocation(inverter.getLocation());
            devInverter.setName(inverter.getName());
            devInverter.setUpdateTime(System.currentTimeMillis());
            devInverter.setRemark(inverter.getRemark());
            int count = inverterService.updateById(devInverter);
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,
                    count,inverter.getId());
        }
    }

    @Override
    public ResponseMessage get(Long id) {
        DevInverter devInverter = inverterService.getById(id);
       return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devInverter);
    }

    @Override
    public ResponseMessage list(Integer areaId) {
        List<DevInverter> inverterList = inverterService.getByAreaId(areaId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,inverterList);
    }

    @Override
    public ResponseMessage config(DevInverter inverter) {
        if(inverter.getId() == 0
                || inverter.getGivenPressure() <0
                || inverter.getMaxPressure() <0
                || inverter.getMinPressure() <0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevInverter devInverter = inverterService.getById(inverter.getId());
            InverterControlData inverterControlData = new InverterControlData();
            inverterControlData.setEui(devInverter.getEui());
            inverterControlData.setId(devInverter.getAddr());
            inverterControlData.setGivenPress(inverter.getGivenPressure());
            inverterControlData.setHyperPress(inverter.getMaxPressure());
            inverterControlData.setLowPress(inverter.getMinPressure());
            int result = mqttBlockedSendManager.send(inverterControlData).getCode();
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,result);
        }
    }

    @Override
    public ResponseMessage control(Long id, Integer sw) {
        DevInverter deviceInverter = inverterService.getById(id);
        if (deviceInverter == null) {
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            InverterControlData inverterControlData = new InverterControlData();
            inverterControlData.setEui(deviceInverter.getEui());
            inverterControlData.setId(deviceInverter.getAddr());
            inverterControlData.setVfdControl(sw);
            int result = mqttBlockedSendManager.send(inverterControlData).getCode();
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,
                    result);
        }
    }

    @Override
    public ResponseMessage refresh(Long id) {
        DevInverter devInverter = inverterService.getById(id);

        InverterControlData inverterControlData = new InverterControlData();
        inverterControlData.setEui(devInverter.getEui());
        inverterControlData.setVfdRefresh(1);
        inverterControlData.setId(devInverter.getAddr());
        mqttSendService.send(inverterControlData.generate());
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = inverterService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS, count);
    }
}
